package org.jeecg.modules.sms.service.impl;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.sms.entity.KjjSms;
import org.jeecg.modules.sms.entity.SmsListParms;
import org.jeecg.modules.sms.mapper.SmsMapper;
import org.jeecg.modules.sms.service.SmsSservice;
import org.jeecg.modules.sms.util.CommonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class SmsServiceImpl implements SmsSservice {


    @Resource
    private SmsMapper smsMapper;


    /**
     * 添加短信发送申请
     * @param sms
     * @return
     */
    @Override
    public Result addSms(KjjSms sms) {
        Result result = new Result();
        sms.setId(UUID.randomUUID().toString().replace("-",""));
        int row = smsMapper.addSms(sms);
        if (row>0){
            result.setCode(200);
            result.setMessage("添加成功！");
            result.setResult(sms);
        }else {
            result.setCode(500);
            result.setMessage("添加失败！");
        }
        return result;
    }

    /**
     * 获取短信发送申请列表
     * @param parms
     * @return
     */
    @Override
    public Result getSmsList(SmsListParms parms) {
        Result result = new Result();
        if (CommonUtil.isEmpty(parms)){
            result.setCode(500);
            result.setMessage("请传入查询参数！");
            return result;
        }
        if (!CommonUtil.isEmpty(parms.getContent())){
            parms.setContent("%"+parms.getContent()+"%");
        }
        List<KjjSms> smsList = smsMapper.getSmsList(parms);
        Integer totalCount = smsMapper.getSmsTotalCount(parms);
        Integer totalPage = (totalCount+parms.getSize()-1)/parms.getSize();
        Map map = new HashMap();
        map.put("totalCount",totalCount);
        map.put("data",smsList);
        map.put("totalPage",totalPage);
        result.setCode(200);
        result.setMessage("查询成功！");
        result.setResult(map);
        return result;
    }
}
